package com.batman.baselibrary.utils;

import android.text.TextUtils;

import com.batman.baselibrary.Constant;
import com.batman.baselibrary.data.result.AccountHelper;
import com.batman.baselibrary.data.result.LoginBean;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;

/**
 * 作者：yuhb on 2019-09-14 16:20.
 * 描述：
 */
public class BasePackagePostData {

    private static String version = "V" + Utils.getVersionName();

    private static String addToken(JSONObject root) {
        try {
            LoginBean login = AccountHelper.getUser();
            if (TextUtils.isEmpty(login.getUserId())) {
                root.put("token", "");
            } else {
                root.put("token", login.getToken());
            }
            root.put("version", version);
            return root.toString();

        } catch (JSONException e) {
            e.printStackTrace();
            return root.toString();
        }
    }


    public static RequestBody updateCheckAppVersion() {
        JSONObject root = new JSONObject();
        JSONObject params = new JSONObject();
        try {
            root.put("cmd", "updateCheckAppVersion");
            addToken(root);
            root.put("params", params);
            params.put("platform", "android");
            params.put("version", version);
            params.put("appName", Constant.APPNAME);
            return createRequestBody(root.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static RequestBody createRequestBody(String params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                params);//EncodeUtils.urlEncode((params))
        return body;
    }


}
